package com.kaaction.modules.eb.api;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.utils.StringUtils;
import com.kaaction.common.web.BaseController;
import com.kaaction.modules.eb.entity.Product;
import com.kaaction.modules.eb.entity.ProductClassify;
import com.kaaction.modules.eb.service.ProductClassifyService;
import com.kaaction.modules.eb.service.ProductService;
import com.kaaction.modules.market.entity.Event;
import com.kaaction.modules.market.entity.EventChannel;
import com.kaaction.modules.market.service.EventChannelService;
import com.kaaction.modules.market.service.EventService;

	@Controller
	@RequestMapping(value = "api/v1/product")
	public class ProductApiController  extends BaseController{
		
		@Autowired
		private ProductService productService;
		
		@Autowired
		private ProductClassifyService productClassifyService;
		
		@Autowired
		private EventChannelService eventChannelService;
		
		@Autowired
		private EventService eventService;
		
		@RequestMapping(value = "/detail")
		public void detail(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
			String productId = request.getParameter("productId");
			String eventId = request.getParameter("eventId");
			String channelId = request.getParameter("channelId");
			
			logger.info("当前路径是:   api/v1/product/detail ");
			logger.info("商品编号 = " + productId);
			logger.info("活动编号 = " + eventId);
			logger.info("渠道编号 = " + channelId);
			
			Event event = new Event();
			Product product = new Product();
			
			if(!StringUtils.isBlank(productId)) {
				 product = productService.get(productId);
				if(product != null){
					logger.info("找到对应的商品");
					if(product.getType().compareTo("900") > 0){
						event = eventService.getByProductId(product.getId());
						if(event != null){
							product.setEvent(event);
						}
						//统计渠道的访问数量
						EventChannel eventChannel = eventChannelService.getByEventAndChannel(eventId,channelId);
						if(eventChannel != null) {
							int clickNum = 1 + eventChannel.getClickNum();
							eventChannel.setClickNum(clickNum);
							eventChannelService.save(eventChannel);
							logger.info("找到该渠道，[活动"+eventId+",渠道"+channelId+"]访问量增加为:"+clickNum);
						} else {
							logger.info("没找到该渠道:"+"[活动"+eventId+",渠道"+channelId+"]");
						}
					} else {
						logger.info("没有对应的活动");
					}
					
				} else {
					logger.info("没有对应的商品");
				}
			}
			super.writeAjaxResponse(response, product);
		}
		/**
		 * 扩展属性
		 * @param request
		 * @param response
		 * @throws IOException
		 */
		@RequestMapping(value = "/attribute")
		public void attribute(HttpServletRequest request, HttpServletResponse response) throws IOException {
			String productId = request.getParameter("productId");
			if(!StringUtils.isBlank(productId)){
				List<ProductClassify> list = productClassifyService.findListByProductId(productId);
				if(list != null){
					super.writeAjaxResponse(response, list);
				}
			}
		}
		/**
		 * 获取列表
		 * @param request
		 * @param response
		 * @throws IOException
		 */
		@RequestMapping(value = "/list")
		public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
			String productNumStr = request.getParameter("productNum");
			int productNum = 10;
			if(!StringUtils.isBlank(productNumStr)){
				productNum = Integer.parseInt(productNumStr);
			}
			List<Product> list = productService.findList(new Product());
			if(list != null && list.size() > productNum){
				list = list.subList(0, productNum);
			}
			super.writeAjaxResponse(response, list);
		}
		
}
